﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

//------------------------------------------------------------
// shaco Framework
// Copyright © 2017-2025 chang.liu All rights reserved.
// Feedback: 449612236@qq.com
//------------------------------------------------------------

namespace shacoEditor
{
    public class InputDialogWindow : EditorWindow
    {
        string _inputText;
        System.Action<string> _callbackInput;
        System.Action _callbackClose;
        System.Action _callbackOnGUI;

        static public void Open(string title, System.Action<string> callbackInput)
        {
            var window = EditorHelper.GetWindow<InputDialogWindow>(null, true, title);
            window._callbackInput = callbackInput;
        }

        static public InputDialogWindow Open(string title, System.Action callbackOnGUI, System.Action callbackClose)
        {
            var window = EditorHelper.GetWindow<InputDialogWindow>(null, true, title);
            window._callbackOnGUI = callbackOnGUI;
            window._callbackClose = callbackClose;
            return window;
        }

        void OnGUI()
        {
            if (null == _callbackOnGUI)
                _inputText = GUILayout.TextField(_inputText);
            else
                _callbackOnGUI();

            GUILayout.Space(Mathf.Min(20, this.position.height / 5));

            if (GUILayout.Button("OK", GUILayout.ExpandHeight(true)))
            {
                OnOKCallBack();
            }

            UpdateEvent();
        }

        void UpdateEvent()
        {
            if (Event.current.type == EventType.KeyUp && (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter))
            {
                OnOKCallBack();
            }
        }

        void OnOKCallBack()
        {
            if (null != _callbackInput)
                _callbackInput(_inputText);

            if (null != _callbackClose)
                _callbackClose();

            this.Close();
        }
    }
}